﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.ComponentModel;
using Skynet.BusinessEntities;
using Skynet.BusinessLogic.BusinessComponents;
using Skynet.WebFoundation.Services.Session;
using System.Xml.Linq;
using Infrastructure.Aspect.Dynamic;
using System.Web.UI.WebControls;
using Skynet.WebFoundation.Services.Navigation;
using System.Web;
using Skynet.WebFoundation.Constants;

namespace Skynet.WebAdministracion.Views.CategoriesMgm
{
    [System.ComponentModel.DataObject]
    public class CategoriesMgmPresenter : Infrastructure.Web.Views.Master.MasterPresenter<ICategoriesMgmView>
    {
        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }

        [Dependency]
        public ISessionService SessionService { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }

        [Dependency]
        public INavigationService NavigationService { get; set; }


        [Dependency]
        public BusinessLogic.BusinessComponents.IBCSecurityQueries BCSecurityQueries { get; set; }

        public override void OnViewInitialized()
        {
            base.OnViewInitialized();
        }
        public override void OnViewLoaded()
        {
            base.OnViewLoaded();
        }
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IEnumerable<dynamic> GetCategorias()  
        {
            var result = this.BCExamQueries.QueryExamCategories(options: QueryExamCategoriesOptions.MaintenanceView, CustomerId: this.SessionService.CustomerId);

            if (string.IsNullOrWhiteSpace(result))
                return null;
            var query = (from c in XElement.Parse(result).Descendants("ExamCategory")
                         select new CustomXmlExpandoObject()
                         {
                             Id = c.Element("Id").Value,
                             CreatedBy = this.SessionService.CustomerId
                         }
                             .BString("Title", c.Element("Title")).BString("Description", c.Element("Description")).BString("NumberExams", c.Element("NumberExams"))).ToList();
            return query;
            //var cat = this.BCExamQueries.SearchExamCategories(options: SearchExamCategoriesOptions.GetAll);
            //return cat;
        }
        [DataObjectMethod(DataObjectMethodType.Insert, false)]
        public void Add(ExamCategory item)
        {           
            
            item.Id = Guid.NewGuid();
            item.CreatedBy = this.SessionService.CustomerId;  
            this.BCExams.MaintenanceExamCategories(new List<ExamCategory>() { item });
            this.NavigationService.NavigateRoute(ViewNames.Support.Names.SucessMessage());            
        }
        [DataObjectMethod(DataObjectMethodType.Update, false)]
        public void Update(ExamCategory item)
        {
            
            item.MarkAsModified();
            this.BCExams.MaintenanceExamCategories(new List<ExamCategory>() { item });
        }
        [DataObjectMethod(DataObjectMethodType.Delete, false)]
        public void Delete(ExamCategory item)
        {
            item.MarkAsDeleted();
            this.BCExams.MaintenanceExamCategories(new List<ExamCategory>() { item }); 
        }
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public ExamCategory GetOne(Guid Id)
        {
            if (Id == Guid.Empty) return null;
            return this.BCExamQueries.SearchExamCategories( SearchExamCategoriesOptions.GetOne, Id).SingleOrDefault(); 
        }
    }
}
